package yunquan.wang.base.report;

import static net.sf.dynamicreports.report.builder.DynamicReports.cmp;
import static net.sf.dynamicreports.report.builder.DynamicReports.col;
import static net.sf.dynamicreports.report.builder.DynamicReports.grp;
import static net.sf.dynamicreports.report.builder.DynamicReports.report;
import static net.sf.dynamicreports.report.builder.DynamicReports.sbt;
import static net.sf.dynamicreports.report.builder.DynamicReports.type;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.component.PageXofYBuilder;
import net.sf.dynamicreports.report.builder.component.TextFieldBuilder;
import net.sf.dynamicreports.report.builder.group.ColumnGroupBuilder;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.definition.datatype.DRIDataType;

import org.apache.commons.lang3.StringUtils;

import yunquan.wang.base.domain.Comment;
import yunquan.wang.base.domain.IdEntity;
import yunquan.wang.base.spring.SpringContextHolder;
import yunquan.wang.modules.base.entity.DynamicColumn;
import yunquan.wang.modules.base.entity.DynamicReport;

import com.google.common.collect.Lists;
@SuppressWarnings({"rawtypes","unchecked"})
public class ReportBuildUtils {

	
	public static JasperReportBuilder buildReport (DynamicReport dreport) throws Exception
	{
		JasperReportBuilder report = report();
		report.setTemplate(Templates.reportTemplate);
		List<DynamicColumn> reportColumns = dreport.getColumns();
		Map<String, TextColumnBuilder> drColumns = new HashMap<String, TextColumnBuilder>();
		for (DynamicColumn column : reportColumns) {
			TextColumnBuilder drColumn = col.column(column.getTitle(),
					column.getName(),
					(DRIDataType) type.detectType(column.getType()));
			drColumn.setPattern(column.getPattern());
			// 默认都居中
			drColumn.setHorizontalAlignment(HorizontalAlignment.CENTER);
			drColumns.put(column.getName(), drColumn);
			report.columns(drColumn);
		}
		if (StringUtils.isNotBlank(dreport.getSubtotals())) {
			for (String subtotal : dreport.getSubtotals().split(",")) {
				report.subtotalsAtSummary(sbt.sum(drColumns.get(subtotal)));
			}
		}
		if (dreport.getTitle() != null) {
			TextFieldBuilder<String> title = cmp.text(dreport.getTitle())
					.setStyle(Templates.bold12CenteredStyle)
					.setHorizontalAlignment(HorizontalAlignment.CENTER);
			report.addTitle(title);
		}
		if (dreport.isShowPageNumber()) {
			PageXofYBuilder pageXofY = cmp.pageXofY().setStyle(
					Templates.boldCenteredStyle);
			report.addPageFooter(pageXofY);
		}
		return report;
	 
	}
	
	public static JasperReportBuilder buildReport(DynamicReport dreport,
			Class pojo) throws Exception {

		dreport.setColumns(getReportColumns(pojo));
		JasperReportBuilder report = report();
		report.setTemplate(Templates.reportTemplate);
		List<DynamicColumn> reportColumns = dreport.getColumns();
	
		Map<String, TextColumnBuilder> drColumns = new HashMap<String, TextColumnBuilder>();
		for (DynamicColumn column : reportColumns) {
		
			TextColumnBuilder drColumn = col.column(column.getTitle(),
					column.getName(),
					(DRIDataType) type.detectType(column.getType()));
			// 如果是以码表开头
			String pattern = column.getPattern();
			if (pattern.startsWith(Comment.CODE_TYPE)) {
				pattern = pattern.substring(Comment.CODE_TYPE.length());
				drColumn.setValueFormatter(new CodeValueFormatter(pattern));
			} else {
				drColumn.setPattern(column.getPattern());
			}
			// 默认都居中
			drColumn.setHorizontalAlignment(HorizontalAlignment.CENTER);
			drColumns.put(column.getName(), drColumn);
			report.columns(drColumn);
		}
		if (StringUtils.isNotBlank(dreport.getGroups())) {
			for (String group : dreport.getGroups().split(",")) {
				ColumnGroupBuilder group2 = grp.group(drColumns.get(group));
				report.groupBy(group2);

				for (String subtotal : dreport.getSubtotals().split(",")) {
					report.subtotalsAtGroupFooter(group2,
							sbt.sum(drColumns.get(subtotal)));
				}
			}
		}
		if (StringUtils.isNotBlank(dreport.getSubtotals())) {
			for (String subtotal : dreport.getSubtotals().split(",")) {
				report.subtotalsAtSummary(sbt.sum(drColumns.get(subtotal)));
			}
		}
		if (dreport.getTitle() != null) {
			TextFieldBuilder<String> title = cmp.text(dreport.getTitle())
					.setStyle(Templates.bold12CenteredStyle)
					.setHorizontalAlignment(HorizontalAlignment.CENTER);
			report.addTitle(title);
		}
		if (dreport.isShowPageNumber()) {
			PageXofYBuilder pageXofY = cmp.pageXofY().setStyle(
					Templates.boldCenteredStyle);
			report.addPageFooter(pageXofY);
		}
		return report;
	}

	public static List<DynamicColumn> getReportColumns(Class pojo) {
		Entity entity = (Entity) pojo.getAnnotation(javax.persistence.Entity.class);
		// 实体类的名字，用以构成国际化字段名称的前缀
		String name = entity.name();
        //要导出的列的列表
		List<DynamicColumn> columns = Lists.newArrayList();
		//获取到类的所有字段
		Field[] fields = pojo.getDeclaredFields();
		//循环处理每个字段
		for (Field field : fields) {
			//获取到字段上的特殊标记
			Comment comment = field.getAnnotation(yunquan.wang.base.domain.Comment.class);
			//如果标记了声明，则表示为需要导出的，如果没有。则为忽略值
			if (comment != null) {
				if (comment.export()) {
					if(field.getType().getGenericSuperclass() instanceof IdEntity) 
					{
						
					}
//					if(field.getType().is)
//					{
//						
//					}
					DynamicColumn colum = new DynamicColumn();
					String titleName = comment.value();
					if (StringUtils.isBlank(titleName)) {
						titleName = name + "." + field.getName();
					}
					colum.setTitle(SpringContextHolder.getMessage(titleName));
					String pattern = comment.pattern();
					colum.setPattern(pattern);

					colum.setName(field.getName());
					colum.setType(field.getType().getSimpleName());
					columns.add(colum);
				}
			}
		}

		return columns;

	}
	// AbstractValueFormatter 码表的格式化
}
